package com.jvtd.app;

import android.app.Activity;
import android.content.Context;
import android.support.multidex.MultiDexApplication;

import com.jvtd.utils.CrashUtils;
import com.jvtd.utils.SystemUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/10/16.
 */

public abstract class JvtdApplication extends MultiDexApplication
{
  private static List<Activity> mActivitys;
  
  public static synchronized JvtdApplication get(Context context)
  {
    return (JvtdApplication) context.getApplicationContext();
  }

  @Override
  public void onCreate() {
    super.onCreate();
    SystemUtils.closeAndroidPDialog();
    configGlobalInit();
  }

  /**
   * 配置APP全局初始化环境
   */
  public abstract void configGlobalInit();

  /**
   * 配置开发环境工具
   *
   * @author Chenlei
   * created at 2018/9/19
   **/
  public void configDevelopment(){
    CrashUtils.initCrashLog(this);
  }

  /**
   * 添加activity
   *
   * @author Chenlei
   * created at 2018/9/19
   **/
  public void addActivity(Activity activity)
  {
    if (mActivitys == null)
      mActivitys = new ArrayList<>();
    mActivitys.add(activity);
  }

  /**
   * 移除activity
   *
   * @author Chenlei
   * created at 2018/9/19
   **/
  public void removeActivity(Activity activity)
  {
    if (mActivitys != null)
      mActivitys.remove(activity);
  }

  /**
   * 退出应用
   *
   * @author Chenlei
   * created at 2018/9/19
   **/
  public void exitApp()
  {
    if (mActivitys != null)
    {
      synchronized (JvtdApplication.class)
      {
        for (Activity mActivity : mActivitys)
          mActivity.finish();
      }
    }
    //结束进程
    android.os.Process.killProcess(android.os.Process.myPid());
    System.exit(0);
  }
}
